package com.IntelligenceInsight.Myth.service;

import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class JobMgrService {

    public static void submit(String model, String dataset, String params){

        Process proc;
        try {
            String[] args = new String[]{
                    "C:\\Users\\cheng\\Anaconda3\\python.exe",
                    "I:\\others\\Myth\\Myth\\Myth\\ml\\api.py",
                    String.valueOf(model),
                    String.valueOf(dataset),
                    String.valueOf(params)
            };
            proc = Runtime.getRuntime().exec(args);
            proc.waitFor();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void submitQueue(String uuid, String modelUuid, String modelString, String filePath, String resultPath){
        System.out.println("submit job "+ uuid +" to hadoop or spark success");
        System.out.println("Job: " + uuid + ", modelUuid: " + modelUuid + ", filePath: " + filePath);

        Process proc;
        try {
            String[] args = new String[]{
                   // "python3",
                   // "/data/BUCT_II/dl/cg.py",
                  "python3",
                  "/Users/jochen/Documents/lab/myth/Myth/ml/api.py",
                    String.valueOf(modelString),
                    String.valueOf(filePath)
            };
            proc = Runtime.getRuntime().exec(args);

            HDFSService hdfsService = new HDFSService();
            hdfsService.writeStreamToHDFS(proc.getInputStream(), resultPath);

//            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
//            String variable = null;
//            while ((variable = in.readLine()) != null) {
//                System.out.println(variable);
//            }
//            in.close();

            proc.waitFor();
            System.out.println("Job: " + uuid + " finished.");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
